Import diddy
Import model

Class MessageWindow
	Public 
	
	Const TOUCH_DELAY:Int = 1
	Const SHOW_STATE:Int = 0
	Const OPENING_STATE:Int = 1
	Const CLOSING_STATE:Int = 2
	Const CLOSED_STATE:Int = 3
	
	Field x#, y#
	Field width, height
	Field message:String
	Field lines:String[]
	Field image:GameImage
	Field callback:WindowCallback = null
	Field touchDelay:Int = 0 ' delay prevents the window being touch closed without seeing it'
	Field state:Int =CLOSED_STATE
	Field zoom:Float=1
	
	Field openingFadeTween:Tween2= New Tween2(Tween2.Elastic.EaseOut,0,1,1500)
	Field closingFadeTween:Tween2= New Tween2(Tween2.Cubic.EaseOut,1,0,500)
	
	Field x1:Int 
	Field y1:Int
	Field x2:Int
	Field y2:Int
		
	Method New()
		
		image=game.images.Find("SpeechBubble")

	End

	Method Show(message:String, callback:WindowCallback)
		self.callback = callback
		self.message=message
		Show()
	End

	Method Show(message:String)
		self.callback = null
		self.message=message
		Show()
	End

	Method Show()
	
		if(state=CLOSED_STATE)
			lines=message.Split(";")
			width = 0
			For Local i:=0 To lines.Length()-1
				width = Max(width, (lines[i].Length()*20)+image.w)
			Next
			height=(lines.Length()*20)+100
			
			x=SCREEN_WIDTH2-width/2
			y=(SCREEN_HEIGHT2)-height/2
			touchDelay = 0
			
			state=OPENING_STATE
			
			x1 = -(width/2)
			y1 = -(height/2)
			x2 = -(width/2)+width
			y2 = -(height/2)+height
		Endif

		openingFadeTween.Start()
	End
	
	Method Hide()
		state=CLOSING_STATE
		closingFadeTween.Start()
	End
	
	Method Update()
		if(state=OPENING_STATE)
			openingFadeTween.Update()
			zoom=openingFadeTween.Value()
			
			if(openingFadeTween.isActive=false)
				state=SHOW_STATE				
			End
		elseif(state=CLOSING_STATE)
			closingFadeTween.Update()
			zoom=closingFadeTween.Value()
			
			if(closingFadeTween.isActive=false)
				state=CLOSED_STATE				
			End
		End
		
		touchDelay+=1

		if((state=SHOW_STATE) and game.TouchHit() and touchDelay>TOUCH_DELAY)			
			touchDelay = 0
			Hide()
			if(callback<>null)
				callback.onClose()
				callback=null
			endif
		Endif	
		

	End
	
	Method Draw:Void(offsetx#=0, offsety#=0)

		
		if(state=CLOSED_STATE)
			return
		End
		
		PushMatrix
		
		Translate x+width/2, y+height/2
		Scale (zoom, zoom)
		
		SetColor 220, 220, 220
		SetAlpha 0.9
		
		
		DrawRect x1,y1, width, height
		SetAlpha 1
		
		SetColor 25,25,25
		DrawLine x1,y1,x2,y1 ' top
		DrawLine x1,y2,x2,y2 'bottom
		DrawLine x1,y1,x1,y2 ' left
		DrawLine x2,y1,x2,y2 'right
		
		SetColor 255,255,255
		
		For Local i:=0 To lines.Length()-1
			DrawText lines[i],image.w+5-(width/2),-(((lines.Length()-1)*20)/2)+i*20,0,0
		'	DrawText lines[i],image.w+5,(((lines.Length()-1)*20)/2)+i*20,0,0
		Next
		
		image.Draw(image.w2-(width/2),0)

		SetColor 255,255,255
		SetAlpha 1
		PopMatrix
	End
End ' class

Interface WindowCallback
Method onClose:void()
End
